package com.xbao.core.util;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by qingshengzheng on 2019/1/20.
 */
public class MyBeanUtil {

    public static <T> T copyProperties(Object entity, Class<T> modelClass) {
        Object model = null;
        if (entity == null || modelClass == null) {
            return null;
        }
        try {
            model = modelClass.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        BeanUtils.copyProperties(entity, model);
        return (T) model;
    }

    public static <T> List<T> copyList(List<?> sources, Class<T> editable) {
        List<T> vs = new ArrayList<T>();
        Object model = null;
        try {
            model = editable.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        for (Object o : sources) {
            T target = copyProperties(o, editable);
            vs.add(target);
        }
        return vs;


    }

}
